package com.cyyl.updater

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.coroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collect

class MainActivity : AppCompatActivity() {

    val downloadUrlE: TextView by lazy {
        findViewById(R.id.progress_text)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<View>(R.id.start_download).setOnClickListener {
            startActivity(Intent(this, SettingActivity::class.java))
        }
        MainScope()

    }

    override fun onResume() {
        super.onResume()
        lifecycle.coroutineScope.launchWhenResumed {
            AppUpdater.lastDownloadFlow?.collect {
                if (it.first == -1) {
                    return@collect
                }
                when (it.first) {
                    AppUpdater.DOWNLOAD_STATUS_RUNNING -> {
                        downloadUrlE.text = it.second.toString()
                    }
                    AppUpdater.DOWNLOAD_STATUS_SUCCESSFUL -> {
                        downloadUrlE.text = "SUCCESSFUL"
                    }
                    else -> {
                        downloadUrlE.text = "Failure:${it.third}"
                    }
                }
            }
        }
    }
}